package com.aiti.lulian.strategy.task;


import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardUserIncreaseRankDto;
import com.aiti.lulian.entity.CardRankTaskUserComplete;
import com.aiti.lulian.entity.newCard.CardDescription;
import com.aiti.lulian.enums.RankIncreaseTaskStrategyEnum;
import com.aiti.lulian.mapper.CardRankTaskUserCompleteMapper;
import com.aiti.lulian.mapper.newCard.CardDescriptionMapper;
import com.aiti.lulian.mapper.newCard.CardPersonalBaseInfoMapper;
import com.aiti.lulian.service.CardUserRankTaskHistoryService;
import com.aiti.lulian.vo.CardRankTaskUserVo;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class CompleteSelfIntroduceStrategy implements RankIncreaseTaskStrategy {
    @Autowired
    private CardRankTaskUserCompleteMapper cardRankTaskUserCompleteMapper;
    @Autowired
    private CardUserRankTaskHistoryService cardUserRankTaskHistoryService;
    @Autowired
    private CardPersonalBaseInfoMapper cardBaseInfoMapper;
    @Autowired
    private CardDescriptionMapper cardDescriptionMapper;

    @Override
    public RankIncreaseTaskStrategyEnum getStrategy() {
        return RankIncreaseTaskStrategyEnum.COMPLETE_SELF_INTRODUCE;
    }

    @Override
    public void handleTask(CardRankTaskUserVo vo, CardUserIncreaseRankDto dto) {
        CardDescription cardDescription = cardDescriptionMapper.queryByBaseInfoId(dto.getCardId());
        if(cardDescription == null || StringUtils.isEmpty(cardDescription.getPersonalDescription()) || "[]".equals(cardDescription.getPersonalDescription())){
            vo.setDoneFlag(false);
        }else{
            vo.setDoneFlag(true);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message handle(CardUserIncreaseRankDto dto) {
        String userId = dto.getUserId();
        String cardId = dto.getCardId();
        List<CardRankTaskUserVo> cardRankTaskUserVos = getCompletedTask(dto);
        if(CollectionUtils.isEmpty(cardRankTaskUserVos)){
            return Message.fail("任务不存在");
        }else{
            if(cardRankTaskUserVos.get(0).isCompleteFlag()) {//已完成
                return Message.fail("该用户已完成过该任务，请勿重复");
            }else{
                CardDescription cardDescription = cardDescriptionMapper.queryByBaseInfoId(cardId);
                if(cardDescription == null || StringUtils.isEmpty(cardDescription.getPersonalDescription()) || "[]".equals(cardDescription.getPersonalDescription())){
                    return Message.fail("领取失败，该用户未完善个人介绍");
                }


                CardRankTaskUserComplete cardRankTaskUserComplete = new CardRankTaskUserComplete();
                cardRankTaskUserComplete.setCardId(cardId);
                cardRankTaskUserComplete.setUserId(userId);
                cardRankTaskUserComplete.setTaskId(cardRankTaskUserVos.get(0).getBaseId());
                cardRankTaskUserCompleteMapper.insert(cardRankTaskUserComplete);
//                LambdaQueryWrapper<CardUserRankTaskHistory> wrapper = new LambdaQueryWrapper<CardUserRankTaskHistory>().eq(CardUserRankTaskHistory::getCardId, cardId)
//                        .eq(CardUserRankTaskHistory::getType, RankIncreaseTaskStrategyEnum.REAL_NAME_AUTH.getType());
//                List<CardUserRankTaskHistory> list = cardUserRankTaskHistoryService.list(wrapper);
//                if(!CollectionUtils.isEmpty(list)){
//                    return Message.fail("该用户已领取过奖励，请勿重复领取");
//                }


                Long score = cardRankTaskUserVos.get(0).getScore();
                dto.setScore(score);
                increseScore(dto);
                recordHistory(dto);


                return Message.success();
            }
        }
    }

    @Override
    public CardRankTaskUserCompleteMapper getCardRankTaskUserCompleteMapper() {
        return cardRankTaskUserCompleteMapper;
    }

    @Override
    public CardPersonalBaseInfoMapper getBaseInfoMapper() {
        return cardBaseInfoMapper;
    }

    @Override
    public CardUserRankTaskHistoryService getHisotryService() {
        return cardUserRankTaskHistoryService;
    }


}